package br.usp.ime.amazon;

import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.json.JsonHttpRequest;
import com.google.api.client.http.json.JsonHttpRequestInitializer;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.books.Books;
import com.google.api.services.books.model.VolumeSaleInfo;
import com.google.api.services.books.model.VolumeVolumeInfo;
import com.google.api.services.books.model.Volumes;
import com.google.api.services.books.model.Volume;
import com.google.api.services.books.Books.Volumes.List;
import com.google.api.services.books.BooksRequest;

public class GoogleBooksMain {
	 public static void main(String[] args) {
	        JsonFactory jsonFactory = new JacksonFactory();
	        try {

	            String prefix = "";
	            String busca = "java+inauthor:deitel";
	            String query = prefix + busca;

	            try {
	                // Set up Books client.
	                final Books books = Books
	                        .builder(new NetHttpTransport(), jsonFactory)
	                        .setApplicationName("Google-BooksSample/1.0")
	                        .setJsonHttpRequestInitializer(
	                                new JsonHttpRequestInitializer() {
	                                    @Override
	                                    public void initialize(
	                                            JsonHttpRequest request) {
	                                        BooksRequest booksRequest = (BooksRequest) request;
	                                        booksRequest.setKey("AIzaSyAJDvhMhPWMH6wnfW2AYDPUp4urYC-DEvc");
	                                    }
	                                }).build();

	                List volumesList = books.volumes().list(query);
	                // volumesList.setFilter("ebooks"); //Valid values: ebooks,
	                // free-ebooks, full, paid-ebooks, partial

	                volumesList.setMaxResults((long) 10);
	                volumesList.setStartIndex((long) 20);

	                // Execute the query.
	                
	                
	                
	                Volumes volumes = volumesList.execute();
	                System.out.println("Tamanho: " + volumes.getTotalItems());
	                if (volumes.getTotalItems() == 0 || volumes.getItems() == null) {
	                    System.out.println("No matches found.");
	                    return;
	                }

	                // Output results.
	                for (Volume volume : volumes.getItems()) {
	                    VolumeVolumeInfo volumeInfo = volume.getVolumeInfo();
	                    VolumeSaleInfo saleInfo = volume.getSaleInfo();
	                    
	                    //titulo
	                    System.out.println("Title: " + volumeInfo.getTitle());
	                    //autores
	                    java.util.List<String> authors = volumeInfo.getAuthors();
	                    if (authors != null && !authors.isEmpty()) {
	                        System.out.print("Author(s): ");
	                        for (int i = 0; i < authors.size(); ++i) {
	                          System.out.print(authors.get(i));
	                          if (i < authors.size() - 1) {
	                            System.out.print(", ");
	                          }
	                        }
	                        System.out.println();
	                    }
	                    
	                }

	            } catch (GoogleJsonResponseException e) {
	                // message already includes parsed response
	                System.err.println(e.getMessage());
	            } catch (HttpResponseException e) {
	                // message doesn't include parsed response
	                System.err.println(e.getMessage());
	                System.err.println(e.getResponse().parseAsString());
	            }
	        } catch (Throwable t) {
	            t.printStackTrace();
	        }
	        System.exit(0);
	    }

}
